import 'package:coin_genarate/coin_generate.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lang/flutter_lang.dart';
import 'package:flutter_theme/theme.dart';
import 'package:flutter_theme/theme_utils.dart';
import 'package:lifecycle_monitor/lifecycle_monitor.dart';
import 'package:route_manager/wx_navigator.dart';
import 'package:wallet_authentication/PIN/models/wallet_info_model.dart';
import 'package:wallet_authentication/mnemonic/widgets/common_visibility_widget.dart';
import 'package:wallet_authentication/module_config.dart';
import 'package:wallet_authentication/utils/aes_util.dart';

class BackupMnemonicWalletScreen extends StatefulWidget {
//  FIXME: 这里怎么不用枚举
  /// 0：按钮和文字都显示，1：只显示按钮，2：按钮文字都不显示
  final int showType;

  const BackupMnemonicWalletScreen({Key key, this.showType = 0})
      : super(key: key);

  @override
  _BackupMnemonicWalletScreenState createState() =>
      _BackupMnemonicWalletScreenState();
}

class _BackupMnemonicWalletScreenState
    extends State<BackupMnemonicWalletScreen> {
  List<String> randomMnemonicList = [];
  String randomMnemonic = "";

  @override
  void initState() {
    super.initState();

    if (AESUtil.decryptString(SpUtil.getString("mnemonic_list")).isEmpty) {
      //首次进mnemonic
      randomMnemonic = CoinGenerateManager().generateMnemonic();
      print('randomMnemonic---' + randomMnemonic);
      randomMnemonicList = randomMnemonic.split(" ");
    } else {
      randomMnemonic = AESUtil.decryptString(SpUtil.getString("mnemonic_list"));
      print('randomMnemonicCache---' + randomMnemonic);
      randomMnemonicList = randomMnemonic.split(" ");
    }
  }

  @override
  Widget build(BuildContext context) {
    VisibilityFlag showButton;
    VisibilityFlag showText;

    if (widget.showType == 0) {
      showButton = VisibilityFlag.visible;
      showText = VisibilityFlag.visible;
    } else if (widget.showType == 1) {
      showButton = VisibilityFlag.visible;
      showText = VisibilityFlag.gone;
    } else if (widget.showType == 2) {
      showButton = VisibilityFlag.gone;
      showText = VisibilityFlag.gone;
    } else {
      showButton = VisibilityFlag.visible;
      showText = VisibilityFlag.visible;
    }

    return Scaffold(
        appBar: AppBar(
          backgroundColor: WXTheme.$c("authentication_header_background"),
          centerTitle: true,
          elevation: 0,
          title: Text(
            "${WXLang.$t("authentication_backup_mnemonic", package: ModuleConfig.moduleName)}",
            style:
                TextStyle(fontSize: 18, color: WXTheme.$c("authentication_header_icon_text")),
          ),
          brightness: Brightness.dark,
        ),
        body: Container(
          padding: EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              Container(
                margin: EdgeInsets.fromLTRB(15, 5, 15, 0),
                child: Text(
                  "${WXLang.$t("authentication_backup_mnemonic_info", package: ModuleConfig.moduleName)}",
                  style: TextStyle(
                    fontSize: 15,
                    color: WXTheme.$c("authentication_list_item_text"),
                  ),
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Container(
                decoration: BoxDecoration(
                    color:  WXTheme.$c("authentication_list_item_color"),
                    border: Border.all(
                        color: WXTheme.$c("authentication_common_text_title"),
                        width: 0.5),
                    borderRadius: BorderRadius.circular(8)),
                margin: EdgeInsets.symmetric(
                  vertical: 10,
                ),
                padding: EdgeInsets.symmetric(vertical: 13, horizontal: 10),
                child: _buildGrid(context),
              ),
              Expanded(
                child: SizedBox(),
              ),
              CommonVisibility(
                visibility: showButton,
                child: Container(
                  margin: EdgeInsets.fromLTRB(0, 0, 0, 20),
                  child: MaterialButton(
                    color: Colors.blue,
                    padding: EdgeInsets.symmetric(horizontal: 50),
                    child: Text(
                      "${WXLang.$t("authentication_backup_mnemonic", package: ModuleConfig.moduleName)}",
                      // style: Theme.of(context).textTheme.headline4,
                      style: TextStyle(
                          color: Color(0xDDFFFFFF),
                          fontWeight: FontWeight.w800,
                          letterSpacing: 1,
                          fontSize: 14),
                    ),
                    onPressed: () {
                      // Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
                      //   return VerifyMnemonicWalletScreen(mnemonicList: randomMnemonicList);
                      // }));
                      // /mnemonicVerify

                      WXNavigator.pushNamed(
                          context: context,
                          routeName: '/mnemonicVerify',
                          arguments: randomMnemonic);
                    },
                  ),
                ),
              ),
              CommonVisibility(
                visibility: showText,
                child: Container(
                  margin: EdgeInsets.fromLTRB(0, 0, 0, 50),
                  child: GestureDetector(
                      onTap: () {
                        // PinCode保存操作 | 保存钱包名称
                        WalletInfo.savePinCodeName();
                        WalletInfo.saveWalletName();

                        /// 注册息屏逻辑
                        StateObserver().registerObserver();
                        SpUtil.putString("mnemonic_list",
                            AESUtil.encryptString(randomMnemonic));

                        //跳主页
                        WXNavigator.pushNamedAndRemoveUntil(
                            "/wallet_home", (route) => false,
                            context: context, moduleName: "wallet_home");
                      },
                      child: Text(
                        "${WXLang.$t("authentication_not_backup", package: ModuleConfig.moduleName)}",
                        style: TextStyle(fontSize: 13),
                      )),
                ),
              )
            ],
          ),
        ));
  }

  Widget _buildGrid(BuildContext context) {
    return GridView.count(
        physics: NeverScrollableScrollPhysics(),
        mainAxisSpacing: 10,
        crossAxisSpacing: 10,
        shrinkWrap: true,
        childAspectRatio: 2.2,
        crossAxisCount: 4,
        children: List.generate(12, (i) {
          return InkWell(
            onTap: () {},
            child: Container(
              decoration: BoxDecoration(
                  color: WXTheme.$c("authentication_mnemonic_grid_item_color"),
                  borderRadius: BorderRadius.circular(2),
                  border: Border.all(color: Color(0x08000000))),
              padding: EdgeInsets.all(1),
              child: Center(
                child: Text(
                  '${randomMnemonicList[i]}',
                  style: TextStyle(
                      fontSize: 12,
                      color: WXTheme.$c("authentication_list_item_text"),
                  ),
                ),
              ),
            ),
          );
        }));
  }
}
